package cn.tju.leavingsystem.web;

import cn.tju.leavingsystem.entity.LeavingApplication;
import cn.tju.leavingsystem.service.LeavingApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/application")
public class LeavingApplicationController {
    @Autowired
    private LeavingApplicationService leavingApplicationService;

    @GetMapping("/")
    private Map<String, Object> listApplication(){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        List<LeavingApplication> list = leavingApplicationService.getLeavingAppliactionList();
        modelMap.put("error_code", 0);
        modelMap.put("message", "Success requesting data");
        modelMap.put("data", list);
        return modelMap;
    }

    @GetMapping("/{applicationId:\\d+}")
    private Map<String, Object> getApplicationById(@PathVariable Integer applicationId){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        LeavingApplication leavingApplication = leavingApplicationService.getLeavingApplicationById(applicationId);
        if (leavingApplication != null){
            modelMap.put("error_code", 0);
            modelMap.put("message", "Success requesting data");
        }else {
            //越界，没有该id的请假单
            modelMap.put("error_code", 1);
            modelMap.put("message", "Coundn't find application, check your applicationId");
        }
        modelMap.put("data", leavingApplication);
        return  modelMap;
    }

    @GetMapping("/staff/{staffId:\\d+}")
    private Map<String, Object> getApplicationByStaffId(@PathVariable Integer staffId){
        Map<String, Object> modelMap = new HashMap<String, Object>();

        List<LeavingApplication> leavingApplication = leavingApplicationService.getLeavingApplicationByStaffId(staffId);
        if(leavingApplication != null){
            modelMap.put("error_code", 0);
            modelMap.put("message", "Success requesting data");
        }else {
            modelMap.put("error_code", 1);
            modelMap.put("message", "Coundn't find application about the staff, check your staffId");
        }
        modelMap.put("data", leavingApplication);
        return modelMap;
    }

    @PostMapping("/")
    private Map<String, Object> addApplication(@ModelAttribute LeavingApplication leavingApplication){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        if(leavingApplicationService.addLeavingApplication(leavingApplication)){
            modelMap.put("error_code", 0);
            modelMap.put("message", "Success inserting application");
        }else {
            modelMap.put("error_code", -1);
            modelMap.put("message", "Fail to insert application");
        }
        return modelMap;
    }

    @PutMapping("/{applicationId:\\d+}")
    private Map<String, Object> approvalApplication(@ModelAttribute LeavingApplication leavingApplication){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        if(leavingApplicationService.modifyLeavingApplication(leavingApplication)){
            modelMap.put("error_code", 0);
            modelMap.put("message", "Success updating application");
        }else {
            modelMap.put("error_code", -1);
            modelMap.put("message", "Fail to update application");
        }
        return modelMap;
    }

    @DeleteMapping("/{applicationId}")
    private Map<String, Object> deleteSelfApplication(@PathVariable Integer applicationId){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        if(leavingApplicationService.removeLeavingApplication(applicationId)){
            modelMap.put("error_code", 0);
            modelMap.put("message", "Success deleting application");
        }else {
            modelMap.put("error_code", -1);
            modelMap.put("message", "Fail to delete application");
        }
        return modelMap;
    }
}
